/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.sdk;

import org.eclipse.core.runtime.IProgressMonitor;
import test.org.zoundry.gaedt.sdk.ZGaeSdkTestCase;

/**
 * A unit test for the SDK updater.
 */
public class ZGaeSDKUpdaterTest extends ZGaeSdkTestCase
{
    /**
     * Tests the updater.
     *
     * @throws Exception
     */
    public void testUpdater() throws Exception
    {
        ZGaeSDKUpdater updater = new ZGaeSDKUpdater();
        ZGaeSDKUpdaterTestProgressMonitor monitor = new ZGaeSDKUpdaterTestProgressMonitor();
        updater.update(monitor);
        String result = monitor.getResult();
        assertEquals(ZGaeSDKUpdaterTest.class.getResource("testUpdater_expected.txt"), result);
    }

    /**
     * A progress monitor that simply records what happened during the update so
     * that it can be later asserted.
     */
    protected static class ZGaeSDKUpdaterTestProgressMonitor implements IProgressMonitor
    {
        /** The string buffer to write to during monitoring. */
        private StringBuffer mBuffer = new StringBuffer();
        /** The last message to be appended to the buffer. */
        private String mLastAppend = null;

        /**
         * Gets the result.
         */
        public String getResult()
        {
            return mBuffer.toString();
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#worked(int)
         */
        public void worked(int aWork)
        {
            appendToBuffer("WORKED: " + aWork);
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
         */
        public void subTask(String aName)
        {
            appendToBuffer("SUBTASK: " + aName);
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
         */
        public void setTaskName(String aName)
        {
            appendToBuffer("SETTASKNAME: " + aName);
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
         */
        public void setCanceled(boolean aValue)
        {
            appendToBuffer("CANCEL: " + aValue);
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
         */
        public boolean isCanceled()
        {
            return false;
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double)
         */
        public void internalWorked(double aWork)
        {
            appendToBuffer("IWORKED: " + aWork);
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#done()
         */
        public void done()
        {
            appendToBuffer("DONE");
        }

        /**
         * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String, int)
         */
        public void beginTask(String aName, int aTotalWork)
        {
            appendToBuffer("BEGIN: " + aName + " WORK: " + aTotalWork);
        }

        /**
         * Appends the message to the buffer.
         *
         * @param aWoaMessagerk
         */
        protected void appendToBuffer(String aMessage)
        {
            if (shouldAppend(aMessage))
            {
                mBuffer.append(aMessage);
                mBuffer.append("\n");
            }
            mLastAppend = aMessage;
        }

        /**
         * Returns true if the given message *should* be appended to the buffer.
         *
         * @param aMessage
         */
        protected boolean shouldAppend(String aMessage)
        {
            return !aMessage.startsWith("SUBTASK: Downloading Google App Engine SDK") || !aMessage.equals(mLastAppend);
        }
    }
}
